#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
#define rep(i, a, b) for (int i = (a); i <= (b); i++)
#define per(i, a, b) for (int i = (a); i >= (b); i--)
#define endl '\n'
const int N = 2e5 + 5;
const ll INF = 2e9;

int n, m, cnt;
ll a[N];
ll b[N];
ll sum[N];
int ans = -INF;
struct Node {
  int pre, nxt;
  int l, r, st;
} lst[N];
int head = 1, tail = 2;
int newnode(int l, int r) {
  static int tot = 0;
  ++tot;
  lst[tot].l = l, lst[tot].r = r;
  lst[tot].st = 1;
  return tot;
}
void insert(int pre, int nxt, int now) {
  lst[pre].nxt = now;
  lst[now].pre = pre;
  lst[nxt].pre = now;
  lst[now].nxt = nxt;
}
int merge(int id) {
  int pre = lst[id].pre, nxt = lst[id].nxt;
  lst[id].st = lst[pre].st = lst[nxt].st = 0;
  int l = lst[pre].l, r = lst[nxt].r;
  int now = newnode(l, r);
  if (pre == head) head = now;
  if (nxt == tail) tail = now;
  insert(lst[pre].pre, lst[nxt].nxt, now);
  return now;
}
using pll = pair<ll, ll>;
priority_queue<pll, vector<pll>, greater<pll>> positive;
priority_queue<pll, vector<pll>, greater<pll>> negative;

void show() {
  for (int p = head; p; p = lst[p].nxt) {
    cout << lst[p].l << ' ' << lst[p].r << ' ' << endl;
  }
  cout << endl;
}
void build_lst() {
  rep(i, 1, n) sum[i] = sum[i - 1] + a[i];
  while (n && a[n] <= 0) n--;
  if (!n) {
    cout << 0;
    exit(0);
  }
  b[1] = -INF;
  int k = 1, l = 1;
  while (a[l] <= 0) l++;
  while (l <= n) {
    int h = l + 1;
    while (h <= n && a[l] * a[h] >= 0) h++;
    b[++k] = sum[h - 1] - sum[l - 1];
    l = h;
  }
  b[++k] = -INF;
  n = k;
  rep(i, 1, n) sum[i] = sum[i - 1] + b[i];
  rep(i, 1, n) {
    newnode(i, i);
    lst[i].pre = i - 1;
    lst[i].nxt = i + 1;
    if (b[i] > 0)
      positive.push(pll(b[i], i)), cnt++;
    else
      negative.push(pll(-b[i], i));
  }

  head = 1, tail = n;
  lst[head].pre = 0, lst[tail].nxt = 0;
}

int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  cin >> n >> m;
  rep(i, 1, n) cin >> a[i];
  build_lst();
  while (cnt > m) {
    pll x = positive.top();
    while (lst[x.second].st == 0) {
      positive.pop();
      x = positive.top();
    }
    pll y = negative.top();
    while (lst[y.second].st == 0) {
      negative.pop();
      y = negative.top();
    }
    if (x.first > y.first) {
      int id = merge(y.second);
      ll v = sum[lst[id].r] - sum[lst[id].l - 1];
      positive.push(pll(v, id));
    } else {
      int id = merge(x.second);
      ll v = sum[lst[id].r] - sum[lst[id].l - 1];
      negative.push(pll(-v, id));
    }
    cnt--;
  }
  ll ans = 0;
  for (int p = head; p; p = lst[p].nxt) {
    int l = lst[p].l, r = lst[p].r;
    ll now = sum[r] - sum[l - 1];
    if (now > 0) ans += now;
  }
  cout << ans;
  return 0;
}